<?php
namespace Home\Controller\Drugs;
use Think\Controller;

class IndexController extends Controller
{
    /*
     * function index
     * 药品展示
     * return void
     */
    public function index()
    {
        if(($username = cookie('username')) && $pwd = cookie('pwd')){
            $drugs = M('drugs');
            $data = $drugs->select();
            $person['name'] = $username;
            $this->assign('person',$person);
            $data = $this->dataFormat($data);
            $this->assign('data',$data);
            return $this->display('Drugs/list');
        }
        return redirect('/drm/');
    }

    private function dataFormat($data)
    {
        foreach ($data as $k => $v) {
            $data[$k]['crash_id'] = $this->getDrugTitle($v['crash_id']);
            $data[$k]['depot_id'] = $this->getDepotTitle($v['depot_id']);
        }
        return $data;
    }

    private function getDrugTitle($id)
    {
        if (!$id) {
           return $data['name'] = null;
        }
        $drugs = M('drugs');
        $data = $drugs->where("id = $id")->find()['name'];
        return $data;
    }

    private function getDepotTitle($id)
    {
         if (!$id) {
           return $data['name'] = null;
        }
        $drugs = M('depot');
        $data = $drugs->where("id = $id")->find()['name'];
        return $data;
    }

    /*
     * function drugAdd
     * 添加药品
     * return bool
     */
    public function drugAdd()
    {
        if(cookie('username') == null || cookie('pwd') == null){
            return redirect('/drm/');
        }
        if(IS_POST){
            $data['name'] = trim(I('POST.name'));
            $data['company'] = trim(I('POST.company'));
            I('POST.crash_id') && $data['crash_id'] = trim(I('POST.crash_id'));
            $data['rest_num'] = trim(I('POST.rest_num'));
            $data['period'] = strtotime(trim(I('POST.period')));
            $data['depot_id'] = (int) trim(I('POST.depot_id'));

            $drugs = M('drugs');
            $res = $drugs->data($data)->add();
            if($res){
                return $this->redirect('Home/Drugs/Index/',array(),1,'页面跳转中');
            }else{
                $this->error('没有修改哦,请重试');
            }
        }
        $depots = $this->getDepot();
        $person['name'] = cookie('username');
        $this->assign('person',$person);
        $this->assign('data',$data);
        $this->assign('type','drugAdd');
        $this->assign('depots',$depots);
        $this->display('Drugs/add');
    }

    /*
     * 删除药品
     * @params id
     * return bool
     */
    public function drugDelete()
    {
        //登录跳转
        if(cookie('username') == null || cookie('pwd') == null){
            return redirect('/drm/');
        }

        //是否存在id
        if(!$id = trim(I('POST.id'))){
            return null;
        }
        $drugs = M('drugs');
        return $drugs->where("id = $id")->delete();
    }

    /*
     * function drugEdit
     * 编辑药品
     * @params id
     * return void
     */
    public function drugEdit()
    {
        //登录跳转
        if(cookie('username') == null || cookie('pwd') == null){
            return redirect('/drm/');
        }

        //是否存在id
        if(!$id = trim(I('id'))){
            return null;
        }

        $drugs = M('drugs');
        if(IS_POST){
            $data = array();
            I('POST.name') && $data['name'] = trim(I('POST.name'));
            I('POST.company') && $data['company'] = trim(I('POST.company'));
            I('POST.crash_id') && $data['crash_id'] = trim(I('POST.crash_id'));
            I('POST.rest_num') && $data['rest_num'] = trim(I('POST.rest_num'));
            I('POST.period') && $data['period'] = strtotime(trim(I('POST.period')));
            I('POST.depot_id') && $data['depot_id'] = trim(I('POST.depot_id'));

            $res = $drugs->where("id = $id")->save($data);
            if($res){
                return $this->redirect('Home/Drugs/Index/',array(),1,'页面跳转中');
            }else{
                $this->error('操作失败，稍后再试','Home/Drugs/Index');
            }
        }

        $drug = $drugs->where("id = $id")->find();//找到需要修改的药品
        $depots = $this->getDepot();//仓库列表
        $person['name'] = cookie('username');
        $this->assign('person',$person);
        $this->assign('type','drugEdit');
        $this->assign('depots',$depots);
        $this->assign('data',$drug);
        $this->display('Drugs/add');
    }

    /**
     * getDepot 
     * 
     * @access private
     * @return array
     */
    private function getDepot()
    {
        $depots = M('depot');//获取仓库
        return $depots->distinct(true)->field('id,name')->select();
    }
}
